package drrr.orm;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.List;

/**
 * Do not instance this class by hand
 * <b>do not use this class in other package</b>
 * Created by Xun on 2015/4/18.
 */
class UserDao extends HibernateDaoSupport {
    private static final Log log = LogFactory.getLog(UserDao.class);

    /**
     * get all of users
     *
     * @return the list of users
     */
    public List getAllEntities() {
        Session session = null;
        try {
            session = this.getSessionFactory().openSession();
            return session.createQuery("from UserEntity").list();
        } finally {
            session.close();
        }
    }

    /**
     * Update the modified userEntity
     *
     * @param userEntity the modified userEntity
     */
    public void update(UserEntity userEntity) {
        Session session = this.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.update(userEntity);
        transaction.commit();
        session.close();
    }

    /**
     * delete the specified userEntity
     *
     * @param userEntity the object which will be deleted
     */
    public void delete(UserEntity userEntity) {
        Session session = this.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.delete(userEntity);
        transaction.commit();
        session.close();
    }

    /**
     * get the userEntity by username
     *
     * @param username the username
     * @return the object if username is exists or null
     */
    public UserEntity getUserEntity(String username) {
        Session session = this.getSessionFactory().openSession();
        List entity = session.createQuery("from UserEntity where username = '" + username + "'").list();
        if (entity.size() == 1) {
            return (UserEntity) entity.get(0);
        } else {
            return null;
        }
    }

    /**
     * get the userEntity by username
     *
     * @param id the user's id
     * @return the object if username is exists or null
     */
    public UserEntity getUserEntity(int id) {
        Session session = this.getSessionFactory().openSession();
        List entity = session.createQuery("from UserEntity where id = " + id).list();
        if (entity.size() == 1) {
            return (UserEntity) entity.get(0);
        } else {
            return null;
        }
    }

    /**
     * insert a new object to the database
     *
     * @param userEntityRef the object will be inserted into the database
     */
    public void insert(UserEntity userEntityRef) {
        Session session = this.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(userEntityRef);
        transaction.commit();
        session.close();
    }
}
